package cn.itcast.blog.service;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import cn.itcast.blog.domain.Category;
import cn.itcast.blog.mapper.CategoryMapper;

public class CategoryService implements CategoryMapper{
	
	SqlSession sqlSession = null;
	
	CategoryMapper categoryMapper = null;
	
	public CategoryService() throws IOException {
		
		SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
		
		//加载配置文件
		InputStream inputStream = Resources.getResourceAsStream("mybatis.xml");
		
		SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
		
		sqlSession = sqlSessionFactory.openSession();
		
		categoryMapper = sqlSession.getMapper(CategoryMapper.class);
	}

	@Override
	public List<Category> findByAll() {
		
		List<Category> findByAll = categoryMapper.findByAll();
		
		return findByAll;
	}

	@Override
	public void insertCategory(Category category) {
		
		categoryMapper.insertCategory(category);
		
		sqlSession.commit();
		
		sqlSession.close();
		
	}
	
	
}
